/**
 * 获取用户的当前设置。返回值中只会出现小程序已经向用户请求过的权限
 * @param scope
 * @param callback
 */
export const getSetting = (
  scope: Array<"scope.userLocation">,
  callback: (auth: (boolean | undefined)[]) => void
) => {
  uni.getSetting({
    success: resp => {
      console.log("getSetting success", resp);
      callback(scope.map(i => resp.authSetting[i]));
    },
    fail: error => {
      console.error("getSetting fail", error);
    },
  });
};

/**
 * 调起客户端小程序设置界面，返回用户设置的操作结果。设置界面只会出现小程序已经向用户请求过的权限
 * @param scope
 * @param callback
 */
export const openSetting = (
  scope: Array<"scope.userLocation">,
  callback: (auth: (boolean | undefined)[]) => void
) => {
  uni.openSetting({
    success: resp => {
      console.log("openSetting success");
      callback(scope.map(i => resp.authSetting[i]));
    },
    fail: error => {
      console.error("openSetting fail", error);
    },
  });
};

/**
 * 提前向用户发起授权请求
 * @param scope
 * @param callback
 */
export const authorize = (
  scope: "scope.userLocation",
  callback: (auth: boolean) => void
) => {
  uni.authorize({
    scope,
    success: () => callback(true),
    fail: () => callback(false),
  });
};
